package tau.dimes.config;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;

public class ConfigurationNode
{
	private final String name;
	private final Map<String, String> attributes;
	private final List<ConfigurationNode> children;
	
	public ConfigurationNode(String name)
	{
		this.name = name;
		this.attributes = new TreeMap<String, String>();
		this.children = new Vector<ConfigurationNode>();
	}

	public String getName()
	{
		return name;
	}

	public Map<String, String> getAttributes()
	{
		return attributes;
	}

	public List<ConfigurationNode> getChildren()
	{
		return children;
	}
	
	public boolean hasAttribute(String name)
	{
		for (String key : attributes.keySet())
			if (name.equalsIgnoreCase(key))
				return true;
		
		return false;
	}
	
	public boolean hasChild(String name)
	{
		for (ConfigurationNode child : children)
			if (name.equalsIgnoreCase(child.getName()))
				return true;
		
		return false;
	}

	public void addChild(ConfigurationNode childNode)
	{
		children.add(childNode);
	}
	
	public List<ConfigurationNode> getChildren(String name)
	{
		List<ConfigurationNode> childList = new Vector<ConfigurationNode>();
		
		for (ConfigurationNode child : children)
			if (name.equalsIgnoreCase(child.getName()))
				childList.add(child);
		
		return childList;
	}

	public ConfigurationNode getChild(String name)
	{
		for (ConfigurationNode child : children)
			if (name.equalsIgnoreCase(child.getName()))
				return child;
		
		return null;
	}
	
	public String getAttribute(String name)
	{
		for (String key : attributes.keySet())
			if (name.equalsIgnoreCase(key))
				return attributes.get(key);
		
		return null;
	}

	public void addAttribute(String key, String value)
	{
		attributes.put(key, value);
	}
}
